package com.esunny.resourcevisualization.service.impl;

import com.esunny.resourcevisualization.domain.entity.User;
import com.esunny.resourcevisualization.domain.entity.UserEntity;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.manage.UserEntityManage;
import com.esunny.resourcevisualization.manage.UserManage;
import com.esunny.resourcevisualization.service.UserEntityService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName UserEntityServiceImpl
 * @Description 逻辑实现接口
 * @Author baochencong
 * @Date 2020/11/5 9:19
 */
@Service
public class UserEntityServiceImpl implements UserEntityService {

    @Resource
    private UserManage userManage;

    @Resource
    private UserEntityManage userEntityManage;

    @Override
    public ReturnStructure addUserEntity(UserEntity userEntity) {
        return userEntityManage.addUserEntity(userEntity);
    }

    @Override
    public ReturnStructure deleteUserEntityByEntityId(int entityId) {
        return userEntityManage.deleteUserEntityByEntityId(entityId);
    }

    @Override
    public ReturnStructure updateUserEntityByEntityId(UserEntity userEntity) {
        int entityId = userEntity.getEntityId();
        if (userEntityManage.getUserEntityByEntityId(entityId) != null){
            return userEntityManage.updateUserEntityByEntityId(userEntity);
        }
        return userEntityManage.addUserEntity(userEntity);
    }

    @Override
    public List<UserEntity> getUserEntityListByUserGroupName(String userGroupName) {
        return userEntityManage.getUserEntityListByUserGroupName(userGroupName);
    }

    @Override
    public UserEntity getUserEntityByEntityId(int entityId) {
        UserEntity userEntityByEntityId = userEntityManage.getUserEntityByEntityId(entityId);
        if (userEntityByEntityId!=null){
            formatUserNameToTitle(userEntityByEntityId);
        }
        return userEntityByEntityId;
    }

    /**
     * 格式化用户名到中文名
     *
     * @param userEntity 用户-实体对应关系信息
     */
    private void formatUserNameToTitle(@NotNull UserEntity userEntity) {
        User userByName = userManage.getUserByName(userEntity.getUsername());
        userEntity.setUsername(userByName.getRealName());
    }
}
